package com.yjt.fresh.activity;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wzgiceman.rxretrofitlibrary.retrofit_rx.Api.BaseResultEntity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.api.ApiCheckVerificationCode;
import com.yjt.fresh.api.ApiGetConfirmCode;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanCity;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 绑定手机号
 * Created by mcc on 2018-07-23.
 */

public class AtyChangeBindPhone extends BaseSimpleActivity {
    @BindView(R.id.back)
    RelativeLayout mBack;
    @BindView(R.id.tvAppAtyBindPhoneStepOne)
    TextView mTvStepOne;
    @BindView(R.id.tvAppAtyBindPhoneStepTwo)
    TextView mTvStepTwo;
    @BindView(R.id.tvAppAtyBindPhoneStepThree)
    TextView mTvStepThree;
    @BindView(R.id.tvAtyBindPhonePhoneNum)
    TextView mTvPhoneNum;
    @BindView(R.id.btnAtyBindPhoneConfirm)
    Button mBtnConfirm;
    @BindView(R.id.edtAtyBindPhoneConfirmCode)
    EditText mEdtConfirmCode;
    @BindView(R.id.btnAtyBindPhoneNext)
    Button mBtnNext;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_bind_phone;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        mTvPhoneNum.setText(getIntent().getStringExtra(MyConstants.PHONE_NUM));
    }

    @Override
    protected void initEvent() {

        mEdtConfirmCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (count > 0){
                    mBtnNext.setClickable(true);
                    mBtnNext.setBackgroundResource(R.drawable.selector_43b624_00b24f_5);
                }else {
                    mBtnNext.setClickable(false);
                    mBtnNext.setBackgroundResource(R.drawable.shape_e6e6e6_5);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        mBtnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getConfirmCode();
            }
        });

        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkConfirmCode();
            }
        });

        mBtnNext.setClickable(false);
    }

    private void checkConfirmCode() {
        ApiCheckVerificationCode apiCheckVerificationCode = new ApiCheckVerificationCode(new HttpOnNextListener<BeanCity>() {
            @Override
            public void onReLogin() {

            }

            @Override
            public void onNext(BeanCity beanCity) {
                Intent intent = new Intent(mContext,AtyChangeBindPhoneNext.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void failed(String massage) {
                super.failed(massage);
                Toast.makeText(mContext, massage, Toast.LENGTH_SHORT).show();
            }
        },mContext,true,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("YZM",mEdtConfirmCode.getText().toString().trim());
        map.put("PHONE", mTvPhoneNum.getText().toString());
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiCheckVerificationCode.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiCheckVerificationCode);
    }

    HttpOnNextListener<BaseResultEntity> mConfirmCodeListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity baseResultEntity) {
            Toast.makeText(mContext, "验证码已发送", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void getConfirmCode(){
        new CountDownTimer(60000,1000){
            @Override
            public void onTick(long l) {
                mBtnConfirm.setEnabled(false);
                mBtnConfirm.setText("获取验证码"+l/1000);
                mBtnConfirm.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
            }

            @Override
            public void onFinish() {
                mBtnConfirm.setEnabled(true);
                mBtnConfirm.setText("获取验证码");
                mBtnConfirm.setTextColor(ContextCompat.getColor(mContext, R.color.color_009944));
            }
        }.start();
        ApiGetConfirmCode apiGetConfirmCode = new ApiGetConfirmCode(mConfirmCodeListener,mContext,false,false,true);
        Map<String,Object> map = new HashMap<>();
        map.put("PHONE", mTvPhoneNum.getText().toString());
        map.put("MESSAGE_TYPE",3);//3是绑定手机号码
        apiGetConfirmCode.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetConfirmCode);
    }

}
